#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# @Time: 2019/4/14  16:49
# @Author: 余浪人
# @email: yulangren520@gmail.com

from wtforms import Form, StringField, validators, SelectField, IntegerField, BooleanField, TextAreaField
from flask import request
from apps.models.column_model import Column_model


# 采集规则
class Collector_Form(Form):
    def __init__(self, *args, **kwargs):
        super(Collector_Form, self).__init__(*args, **kwargs)
        if not Column_model.query.count():
            self.class_lD.choices = [(0, '请先添加分类')]
        else:
            self.class_lD.choices = [(r.id, r.column_name) for r in Column_model.query.all()]  # 初始化栏目

    project_name = StringField(
        '项目名称', validators=[validators.DataRequired('项目名称不可为空!')],
        render_kw={"class": "layui-input", "lay-verify": "required", "style": "width: 20%;float: left;"})
    class_lD = SelectField('所属栏目', validators=[validators.DataRequired('请选择所属栏目!')], coerce=int)
    rule_cls = SelectField('规则类型', choices=[(2, '正则式'), (1, 'Xpath')], coerce=int)
    agreement_cls = SelectField('协议类型', choices=[(1, 'HTTP'), (2, 'HTTPS')], coerce=int)
    goal_url = StringField(
        '拼接URL',  # validators=[validators.URL(message='目标URL格式错误!')],
        render_kw={"class": "layui-input", "lay-verify": "required", "style": "width: 50%;float: left;"})
    fixed_URL = TextAreaField(
        '指定采集',  # validators=[validators.URL(message='URL格式错误!')],
        render_kw={"class": "layui-textarea", "style": "width: 60%;", "placeholder": 'http://'})
    URL_create = StringField(
        'URL生成', render_kw={"class": "layui-input", "style": "width: 60%;float: left;", "placeholder": 'http://'})
    create_range = StringField(
        '生成范围',validators=[validators.Regexp(r'\d+|\d+', message='生成范围格式错误')], render_kw={"class": "layui-input", "style": "width: 20%;float: left;"})
    create_is_range = BooleanField('批量生成', render_kw={"title": "批量生成"})
    img_save_path = StringField(
        '图片文件本地保存目录', validators=[validators.Regexp(r'.*/$', message='保存地址格式错误')], default='images/',
        render_kw={"class": "layui-input", "style": "width: 40%;float: left;", "lay-verify": "required", })
    replace_content = TextAreaField(
        '内容替换', render_kw={"placeholder": "替换前的字符串[%TL%]替换后的字符串", "class": "layui-textarea"})
    list_rule = TextAreaField('列表规则', validators=[validators.DataRequired('列表规则未添加!')],
                              render_kw={"class": "layui-textarea"})
    URL_rule = TextAreaField('链接规则', validators=[validators.DataRequired('列表URL规则未添加!')],
                             render_kw={"class": "layui-textarea", "style": "min-height: 50px;"})
    title_rule = TextAreaField('标题规则', render_kw={"class": "layui-textarea", "style": "min-height: 50px;"})
    content_rule = TextAreaField('内容规则', validators=[validators.DataRequired('内容规则未添加!')],
                                 render_kw={"class": "layui-textarea"})
    keys_rule = TextAreaField('关键词规则', render_kw={"class": "layui-textarea", "style": "width: 80%;min-height: 50px;"})
    author_rule = TextAreaField('作者规则', render_kw={"class": "layui-textarea", "style": "width: 80%;min-height: 50px;"})
    source_rule = TextAreaField('来源规则', render_kw={"class": "layui-textarea", "style": "width: 80%;min-height: 50px;"})
    description_rule = TextAreaField('描述规则',
                                     render_kw={"class": "layui-textarea", "style": "width: 80%;   min-height: 50px;"})
    restrictive = IntegerField('采集限制', default=0,
                               render_kw={"class": "layui-input", "style": "width: 10%;float: left;"})
    img_is_save = BooleanField('保存图片', render_kw={"title": "保存图片"})
    url_is_desc = BooleanField('倒序采集', render_kw={"title": "倒序采集"})
    # tim_acquisition = BooleanField('定时采集', render_kw={"title": "定时采集"})
    url_relative = BooleanField('URL相对地址', render_kw={"title": "URL相对地址"})
    img_relative = BooleanField('图片相对地址', render_kw={"title": "图片相对地址"})
    filterFrame = BooleanField('过滤Iframe', render_kw={"title": "Iframe"})
    filterObject = BooleanField('过滤Object', render_kw={"title": "Object"})
    filterDiv = BooleanField('过滤Div', render_kw={"title": "Div"})
    filterScript = BooleanField('过滤Script', render_kw={"title": "Script"})
    filterStyle = BooleanField('过滤Style', render_kw={"title": "Style"})
    filterTable = BooleanField('过滤Table', render_kw={"title": "Table"})
    filterSpan = BooleanField('过滤Span', render_kw={"title": "Span"})
    filterImg = BooleanField('过滤Img', render_kw={"title": "Image"})
    filterFont = BooleanField('过滤Font', render_kw={"title": "Font"})
    filterA = BooleanField('过滤A', render_kw={"title": "A"})
    filterP = BooleanField('过滤P', render_kw={"title": "P"})
    filterAll = BooleanField('过滤全部', render_kw={"title": "全部"})


# 自动采集
class Auto_motion_Form(Form):
    project_name = StringField(
        '项目名称', validators=[validators.DataRequired('项目名称不可为空!')],
        render_kw={"class": "layui-input", "lay-verify": "required"})
    set_time = IntegerField('间隔时间', validators=[validators.DataRequired('请输入时间!')], render_kw={"class": "layui-input"})
    is_start = BooleanField('状态', render_kw={"title": "运行"})


# 代理IP
class Proxy_Form(Form):
    is_proxy = BooleanField('启用代理IP:', render_kw={"title": "启用"})
    app_key = StringField('APP_KEY:', render_kw={"class": "layui-input", "style": "width:50%"})

    def validate_app_key(self, app_key):
        if request.form.get('is_proxy'):
            app_key = request.form.get('app_key')
            if len(app_key) != 32:
                raise validators.ValidationError("APP_KEY有误!")
